<template>
  <div class="add_modle_dialog">
    <el-dialog title="多人游戏信息设置" :visible.sync="visible" width="30%">
      <el-form :model="form" ref="form">
        <el-form-item label="活动名称" :label-width="formLabelWidth" prop="name" :rules="[rules,rulesLength50]">
          <el-input v-model="form.name" auto-complete="off" placeholder="请输入信息" size="mini" clearable></el-input>
        </el-form-item>
        <el-form-item label="开始日期" :label-width="formLabelWidth" prop="beginDate" :rules="rules2">
          <el-date-picker
            v-model="form.beginDate"
            placeholder="选择日期"
            type="date"
            value-format="yyyy-MM-dd"
            size="mini"
            clearable
            :picker-options="{
              disabledDate(time) {
            return time.getTime() < Date.now()-86400000;
          },
            }">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="开始时间" :label-width="formLabelWidth" prop="beginTime" :rules="rules2">
          <el-time-picker
            v-model="form.beginTime"
            :picker-options="{
              selectableRange: '09:00:00 - 16:00:00',
              format:'HH:mm:ss'
          }"
            value-format="HH:mm:ss"
            placeholder="选择时间"
            size="mini"
            clearable>
          </el-time-picker>
        </el-form-item>
        <el-form-item label="持续时间(分钟)" :label-width="formLabelWidth" prop="activityTime" :rules="rules3">
          <el-input v-model.number="form.activityTime" auto-complete="off" placeholder="请输入" size="mini" min="1" type="number"
                    clearable></el-input>
        </el-form-item>
        <el-form-item label="找寻展品数量" :label-width="formLabelWidth" prop="exhibitNum" :rules="rules3">
          <el-input v-model.number="form.exhibitNum" auto-complete="off" placeholder="请输入" size="mini" min="1" type="number"
                    clearable></el-input>
        </el-form-item>
        <el-form-item label="重复周期" :label-width="formLabelWidth" prop="repeatType" :rules="rules1">
          <el-select v-model="form.repeatType" placeholder="请选择" size="mini">
            <el-option label="一次性" :value="'disposable'"></el-option>
            <el-option label="每天" :value="'day'"></el-option>
            <el-option label="每周" :value="'week'"></el-option>
            <el-option label="每月" :value="'month'"></el-option>
          </el-select>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="close">取 消</el-button>
        <el-button type="primary" @click="confirm('form')" v-if="!isedit">确 定</el-button>
        <el-button type="primary" @click="save('form')" v-if="isedit">保存</el-button>
      </div>
    </el-dialog>
  </div>
</template>
<script>
  import ApiClient from '@/utils/ApiClient'
  import AppSelector from '@/components/AppSelector'
  import ValidataRules from '@/utils/validateRules'


  export default {
    name: 'AddGameDialog',
    props: {
      show: {type: Boolean, default: false},
      dataItem: {type: Object},
      isedit: {type: Boolean, default: false}
    },
    components: {
      'app-selector': AppSelector,
    },
    data() {
      return {
        formLabelWidth: '120px',
        form: {
          name: '',
          beginDate: null,
          beginTime: null,
          activityTime: null,
          exhibitNum: null,
          repeatType: '',
        },
        areaUrl: '/backend/area/list.do',
        modelUrl: '/backend/model/list.do',
        rules: {required: true, message: '不能为空', trigger: 'blur'},
        rules1: {required: true, message: '不能为空', trigger: 'change'},
        rules2: {required: true, message: '不能为空', trigger: 'change'},
        rules3: [
          {required: true, message: '不能为空', trigger: 'blur'},
          { validator: ValidataRules.minNum(1), trigger: 'blur' }
        ],
        rulesLength50: { validator: ValidataRules.limitMaxLength(50), trigger: 'blur' }
      }
    },
    methods: {
      close() {
        this.$emit('update:show', false)
        this.$emit('update:editId', null)
      },
      //操作
      confirm(form) {
        this.$refs[form].validate((valid) => {
          if (valid) {
            let data = _(this.form).clone();
            ApiClient.post('/backend/activity/add.do', this.form).then(() => {
              this.close();
              this.$emit('success')
            })
          } else {
            return false;
          }
        });
      },
      save(form) {
        console.log("save: ", this.form);
        this.$refs[form].validate((valid) => {
          if (valid) {
            ApiClient.put('/backend/activity/update.do', this.form).then(() => {
              this.close();
              this.$emit('success')
            })
          } else {
            return false;
          }
        });
      }
    },
    computed: {
      visible: {
        get: function () {
          return this.show == false ? false : true
        },
        set: function () {
          this.$emit('update:show', false)
          this.$emit('update:editId', null)
        }
      }
    },
    mounted() {
      if (this.dataItem) {
        this.form = _.cloneDeep(this.dataItem);
      }
    }
  }
</script>
<style lang="scss">
  .add_modle_dialog {
    .el-dialog__header {
      text-align: center;
    }
    .el-form-item {
      margin-bottom: 10px;
    }
    .el-form-item__content {
      text-align: left;
    }
  }
</style>

